package cn.wolfcode.service.impl;

import cn.wolfcode.common.web.Result;
import cn.wolfcode.domain.Product;
import cn.wolfcode.domain.SeckillProduct;
import cn.wolfcode.domain.SeckillProductVo;
import cn.wolfcode.feign.ProductFeignApi;
import cn.wolfcode.mapper.SeckillProductMapper;
import cn.wolfcode.mq.MQConstant;
import cn.wolfcode.redis.SeckillRedisKey;
import cn.wolfcode.service.ISeckillProductService;
import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@Service
public class SeckillProductServiceImpl implements ISeckillProductService {
    @Autowired
    private SeckillProductMapper seckillProductMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Autowired
    private ProductFeignApi productFeignApi;

    @Override
    public List<SeckillProductVo> selectByTypes(int time) {
        //先获取到秒杀商品的集合
        List<SeckillProduct> seckillProducts = seckillProductMapper.queryCurrentlySeckillProduct(time);
        //将id进行封装
        List<Long> ids = new ArrayList<>();
        for (SeckillProduct seckillProduct : seckillProducts) {
            ids.add(seckillProduct.getProductId());
        }
        //远程调用商品服务进行获取数据
        Result<List<Product>> result = productFeignApi.selectByIds(ids);
        if (result!=null&&!result.hasError()){
            List<Product> products = result.getData();
            //进行聚合操作
            HashMap<Long, Product> map = new HashMap<>();
            for (Product product : products) {
                map.put(product.getId(), product);
            }
            List<SeckillProductVo> vos = new ArrayList<>();
            for (SeckillProduct seckillProduct : seckillProducts) {
                SeckillProductVo vo = new SeckillProductVo();
                Product product = map.get(seckillProduct.getProductId());
                BeanUtils.copyProperties(product,vo);
                BeanUtils.copyProperties(seckillProduct,vo);
                vos.add(vo);
            }
            return vos;
        }
        return null;
    }

    @Override
    public List<SeckillProductVo> queryByTime(int time) {
        //从Redis中查询秒杀商品集合
        String key = SeckillRedisKey.SECKILL_PRODUCT_LIST.getRealKey(time + "");
        List<Object> values = redisTemplate.opsForHash().values(key);
        List<SeckillProductVo> resultList = new ArrayList<>();
        for(Object value:values){
            resultList.add(JSON.parseObject((String) value, SeckillProductVo.class));
        }
        return resultList;

    }

    @Override
    public SeckillProductVo queryById(int time, Long seckillId) {
        String key = SeckillRedisKey.SECKILL_PRODUCT_LIST.getRealKey(time+"");
        String object = (String) redisTemplate.opsForHash().get(key, seckillId + "");
        return JSON.parseObject(object,SeckillProductVo.class);
    }

    @Override
    public void replenishmentOfInventory(Integer time, Long seckillId) {
        int stockCount = seckillProductMapper.getStockCount(seckillId);
        redisTemplate.opsForHash().put(SeckillRedisKey.SECKILL_STOCK_COUNT_HASH.getRealKey(time+""),seckillId+"",stockCount+"");

    }
}
